# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
from scrapy.pipelines.images import ImagesPipeline
from scrapy.exceptions import DropItem
from scrapy.http import Request
import re


class AoisolasPipeline(ImagesPipeline):
    def get_media_requests(self,item,info):
        for image_url in item['Imgurl']:
            yield Request(image_url, meta={'item': item['name']})

    def file_path(self, request, response=None, info=None):
        # 文件夹名
        name = request.meta['item']
        name = re.sub(r'[？\\*|“<>:/()0123456789]', '', name)
        # 文件名
        image_uid = request.url.split('/')[-1]
        filename = u'full/{0}/{1}'.format(name, image_uid)
        return filename
        # return 'full/name/imageuid'

    def item_completed(self, results, item, info):
        image_path = [x['path'] for ok, x in results if ok]
        if not image_path:
            raise DropItem('Item contains no images')
        item['image_paths'] = image_path
        return item
